package gulimall.controller;

import gulimall.entity.SkuInfo;
import gulimall.service.SkuInfoService;
import gulimall.utils.PageUtils;
import gulimall.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;


/**
 * sku信息
 *
 * @author HLF
 * @email 1132814637@qq.com
 * @date 2022-05-04 15:04:15
 */
@RestController
@RequestMapping("/product/skuinfo")
public class SkuInfoController {
    @Resource
    private SkuInfoService skuInfoService;

    /**
     * sku检索
     * 请求的参数：
     * {
     * page: 1,//当前页码
     * limit: 10,//每页记录数
     * sidx: 'id',//排序字段
     * order: 'asc/desc',//排序方式
     * key: '华为',//检索关键字
     * catelogId: 0,
     * brandId: 0,
     * min: 0,
     * max: 0
     * }
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = skuInfoService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据skuId查询商品单价
     */
    @GetMapping("/getPrice/{skuId}")
    public R getPrice(@PathVariable("skuId") Long skuId) {
        BigDecimal price = skuInfoService.getById(skuId).getPrice();

        return R.ok().put("data", price);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{skuId}")
    public R info(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);

        return R.ok().put("skuInfo", skuInfo);
    }

    @GetMapping("/skuNameById/{skuId}")
    public R skuNameById(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);

        return R.ok().put("skuName", skuInfo.getSkuName());
    }

    /*@GetMapping("/skuPrice")
    public R skuPrice(@RequestParam(value = "skuId") Long skuId) {
        System.out.println(skuId);
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        System.out.println("skuInfo:"+skuInfo);
        BigDecimal price = skuInfo.getPrice();
        return R.ok().put("price", price);
    }*/

    /**
     * 保存商品详细信息
     * 新增商品
     */
    @RequestMapping("/save")
    public R save(@RequestBody SkuInfo skuInfo) {
        skuInfoService.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:skuinfo:update")
    public R update(@RequestBody SkuInfo skuInfo) {
        skuInfoService.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds) {
        skuInfoService.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
